<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import echarts from 'echarts'
import resize from './mixins/resize'
import { statisticByType } from '@/api/statistics/statistics'

require('echarts/theme/macarons') // echarts theme

const animationDuration = 2000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null,
      xAxisData: [],
      totalData: [],
      usageData: [],
      maxValue: undefined,
      minValue: undefined,
      maxAxis: undefined,
      minAxis: undefined
    }
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  created() {
    this.getStatisticByType()
  },
  methods: {
    getStatisticByType() {
      statisticByType().then(response => {
        const data = response.data
        if (data.length > 0) {
          this.maxValue = data[0].used
          this.minValue = data[0].used
          this.maxAxis = 0
          this.minAxis = 0
        }
        data.map((item, index) => {
          this.xAxisData.push(item.name)
          this.totalData.push(item.total)
          this.usageData.push(item.used)
          if (item.used > this.maxValue) {
            this.maxValue = item.used
            this.maxAxis = index
          }
          if (item.used < this.minValue) {
            this.minValue = item.used
            this.minAxis = index
          }
        })
        this.initChart()
      })
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        title: {
          text: '样本统计'
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['数量', '使用数量']
        },
        toolbox: {
          show: true,
          feature: {
            dataView: { show: true, readOnly: false },
            magicType: { show: true, type: ['line', 'bar'] },
            restore: { show: true },
            saveAsImage: { show: true }
          }
        },
        calculable: true,
        xAxis: [
          {
            type: 'category',
            data: this.xAxisData
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        series: [
          {
            name: '数量',
            type: 'bar',
            barMaxWidth: 32,
            data: this.totalData,
            animationDuration,
            markPoint: {
              data: [
                { type: 'max', name: '最大值' },
                { type: 'min', name: '最小值' }
              ]
            }
          },
          {
            name: '使用数量',
            type: 'bar',
            barMaxWidth: 32,
            data: this.usageData,
            animationDuration,
            markPoint: {
              data: [
                { name: '使用最高', value: this.maxValue, xAxis: this.maxAxis, yAxis: this.maxValue },
                { name: '使用最低', value: this.minValue, xAxis: this.minAxis, yAxis: this.minValue }
              ]
            }
          }
        ]
      })
    }
  }
}
</script>
